﻿using Sunny.UI;
using System;
using System.Drawing;
using System.Dynamic;
using System.Threading;
using System.Windows.Forms;

namespace CoreShop.App
{
    /// <summary>
    /// 窗口工具类
    /// 不可在Tools模块中的
    /// </summary>
    public class DialogUtil
    {

        // 通用定位方法
        public void SetFormPosition(UIForm form)
        {
            int x = form.Location.X;
            int y = form.Location.Y;
            int width = form.Width;
            int height = form.Height;
            Screen screen = Screen.FromControl(form);

            x = (screen.Bounds.Width - width) / 2 + screen.Bounds.X;
            y = (screen.Bounds.Height - height) / 2 + screen.Bounds.Y;

            form.Location = new Point(x, y);
        }

        /// <summary>
        /// 开始加载
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="ownerForm">父窗体</param>
        /// <param name="work">待执行工作</param>
        /// <param name="workArg">工作参数</param>
        public static void ShowLoading(string message,
            Form ownerForm,
            ParameterizedThreadStart work,
            object workArg = null)
        {
            LoadingForm loadingForm = new LoadingForm(message);
            dynamic expandoObject = new ExpandoObject();

            expandoObject.Form = loadingForm;
            expandoObject.WorkArg = workArg;
            loadingForm.SetWorkAction(work, expandoObject);
            loadingForm.ShowDialog(ownerForm);
            //loadingForm.Show(ownerForm);

            if (loadingForm.WorkException != null)
            {
                throw loadingForm.WorkException;
            }
        }

        /// <summary>
        /// 支持变化提示信息的加载方法
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="ownerForm">父窗体</param>
        /// <param name="work">待执行工作</param>
        /// /// <param name="del">委托方法</param>
        /// <param name="workArg">工作参数</param>
        /// LogManage.Debug(((dynamic)obj).WorkArg);
        public static void ShowLoadingChangeMessage(string message,
            Form ownerForm,
            ParameterizedThreadStart work,
            ref DelegateClickStr del,
            object workArg = null)
        {
            LoadingForm loadingForm = new LoadingForm(message);
            dynamic expandoObject = new ExpandoObject();

            del += loadingForm.SetText;
            expandoObject.Form = loadingForm;
            expandoObject.WorkArg = workArg;
            loadingForm.SetWorkAction(work, expandoObject);
            loadingForm.Show(ownerForm);
        }
    }
}
